const label = require('../../config/label')
const id = document.cookie.match(RegExp('jxSoftWareCustomerId'+'=([^;]+)'))[1]
const labelType = label['id' + id] || label['id1694237148']
// const labelType = label['id1125681210']   // 瑞谷锦

const printLabel = {
  methods: {
    tscPrint(list){
      const JsBarcode = require('jsbarcode')
      let printBody = document.getElementById('p')
      printBody.innerHTML = ''
      list.forEach((e,i) => {
        if (!e.kindsNumberIngredient) {
          e.kindsNumberIngredient = []
        }
        // console.log(e)
        const num = Number(e.number)
        if (num) {
          // 它色段 
          const colorList = this.printColorMap[e.styleNumber].toString()
          const sizeList = this.printSizeMap[e.styleNumber].toString()
          let others = colorList.replace(e.colorName, '').replace(/,/g, ' ').replace(/色/g, '') + ' ' + sizeList.replace(e.size, '').replace(/,/g, ' ')
          let sizeShape = ''
          if (e.kindsNumber.clothing) {
            // 1女
            sizeShape = e.kindsNumberIngredient.upDown ? e.sizeObj.lowNumberStyle : e.sizeObj.topNumberStyle
          } else {
            // 0男
            sizeShape = e.kindsNumberIngredient.upDown ? e.sizeObj.lowNumberStyle : e.sizeObj.topNumberStyle
          }
          for (let j = 0; j < num; j++) {
            let printItem = document.createElement('div')
            let ingredient = ''
            const ingredientList = [
              'ingredientOne','ingredientTwo','ingredientThree','ingredientFour',
              'ingredientFive','ingredientSix','ingredientSeven','ingredientEight',
              'ingredientNine','ingredientTen'
            ]
            for (const k of ingredientList) {
              if (e.kindsNumberIngredient[k]) {
                ingredient += `<p class="font10">${e.kindsNumberIngredient[k]}</p>`
              } else break
            }
            printItem.innerHTML = labelType.innerHTML({e, i, others, ingredient, sizeShape})
            printBody.appendChild(printItem)
            JsBarcode(
              ".barcode" + i, 
              e.kindsNumber.kindsNumber, 
              {
                displayValue: false,
                height: 36,
                width: 1,
                margin: 4
              }
            )
          }
        }
      })
      let wind = window.open("", 'newwindow', 'height=500, width=1000, top=100, left=100, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no') 
      wind.document.body.innerHTML = printBody.innerHTML 
      
      let style = document.createElement('style') 
      style.type = 'text/css'
      style.media = 'print'
      style.innerHTML = `
        * {
          margin: 0;
          padding: 0;
        }
        .onePage {
          width: 45mm;
          height: 100%;
          font-size: 11px;
          position: relative;
          box-sizing: border-box;
          padding-left: 4mm;
          page-break-before: always;
        }
        .onePage p {
          margin: 0;
          line-height: 110%;
        }
        .bottomBox img {
          width: 20mm;
        }
        .size {
          width: 5mm;
          display: inline-block;
          text-align: center;
        }
        .bold {
          font-weight: bold;
        }
        .center {
          text-align: center;
        }
        .font9 {
          font-size: 9px;
        }
        .font10 {
          font-size: 10px;
        }
        .font13 {
          font-size: 13px;
        }
        .font16 {
          font-size: 16px;
        }
        .title {
          width: 100%;
          text-align: center;
          padding-top: 2mm;
        }
        .bottomBox {
          position: absolute;
          bottom: 1mm;
          left: 0;
          width: 100%;
          padding-left: 4mm;
        }
        
      ` + labelType.style
      wind.document.head.appendChild(style) 
      wind.print()
      return false
    },

    socksLabel (list) {
      const JsBarcode = require('jsbarcode')
      let printBody = document.getElementById('p')
      printBody.innerHTML = ''

      list.forEach((e,i) => {
        console.log(e);
        if (!e.kindsNumberIngredient) {
          e.kindsNumberIngredient = []
        }
        const num = Number(e.number)
        if (num) {
          for (let j = 0; j < num; j++) {
            let printItem = document.createElement('div')
            let ingredient = ''
            if (e.kindsNumberIngredient.ingredientOne) {
              ingredient = `<div class="ingredientBox"><p class="ingredientTitle">${e.kindsNumberIngredient.ingredientOne}</p>`
              const ingredientList = [
                'ingredientTwo', 'ingredientThree', 'ingredientFour', 'ingredientFive'
              ]
              for (const k of ingredientList) {
                if (e.kindsNumberIngredient[k]) {
                  ingredient += `<p>${e.kindsNumberIngredient[k]}</p>`
                } else break
              }
              ingredient += '</div>'
            }
            
            printItem.innerHTML = `
            <div class="onePage">
              <p>品名：${e.kindsNumber.commodityName}</p>
              ${ingredient}
              <p>执行标准：${e.kindsNumberIngredient.standard || ''}</p>
              <p class="edge">
                <span>规格：${e.sizeNo}</span>
                <span>合格证</span>
              </p>
              <p class="edge">
                <span>售价：￥${e.kindsNumber.sellingPrice}</span>
                <span>COL: ${e.kindsNumber.colorNo}</span>
              </p>
              <img class="barcode${i}">
              <div class="bottom bold">
                No. ${e.kindsNumber.styleNumber}
              </div>
            </div>
            `
            printBody.appendChild(printItem)
            if (e.kindsNumberIngredient.ean13No) {
              JsBarcode(
                ".barcode" + i, 
                e.kindsNumberIngredient.ean13No, 
                {
                  format: 'EAN13',
                  height: 30,
                  width: 2,  //设置条之间的宽度
                  margin: 0,
                  displayValue: true,  //是否在条形码下方显示文字
                  textMargin: 0,  //设置条形码和文本之间的间距
                  fontSize: 16,  //设置文本的大小
                }
              )
            }
          }
        }
      })

      let wind = window.open("", 'newwindow', 'height=500, width=1000, top=100, left=100, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no') 
      wind.document.body.innerHTML = printBody.innerHTML 
      
      let style = document.createElement('style') 
      style.type = 'text/css'
      // style.media = 'print'
      style.innerHTML = `
        * {
          margin: 0;
          padding: 0;
        }
        .onePage {
          width: 40mm;
          height: 30mm;
          font-size: 6px;
          position: relative;
          box-sizing: border-box;
          padding: 2mm 2mm 0;
          page-break-before: always;
        }
        .onePage p {
          margin: 0;
          line-height: 135%;
        }
        .bold {
          font-weight: bold;
        }
        .font10 {
          font-size: 10px;
        }
        img {
          padding: 0 5%;
          width: 90%;
        }
        .edge span:last-child {
          float: right;
        }

        .ingredientBox {
          padding-left: 12mm;
          display: flex;
          flex-wrap: wrap;
          justify-content: space-around;
        }
        .ingredientBox p {
          width: 10mm;
          overflow: hidden;
        }
        .ingredientBox p:last-child {
          margin-bottom: 4px;
        }
        .ingredientTitle {
          position: absolute;
          left: 2mm;
        }
        .bottom {
          display: block;
          position: absolute;
          color: #fff;
          background-color: #000;
          width: 100%;
          bottom: 3mm;
          left: 0;
          text-align: center;
          font-size: 3mm;
        }
      `
      //  + labelType.style
      wind.document.head.appendChild(style) 
      wind.print()
      return false
    }
  }
}

export default printLabel